﻿
/// <reference path="/Jquery/jquery-1.4.1-vsdoc.js" />
/// <reference path="/Jquery/jquery-1.4.1.min.js" />
/// <reference path="/Jquery/jquery.jCache.js" />
/// <reference path="/Plugin/editor/tiny_mce/plugins/imagemanager/js/imageManager.js" />
/// <reference path="/Jquery/jquery.alerts.js" />

/// <reference path="../Pages/Product/Item.js" />


/// <reference path="/js/Message.js" />

//-----------------------------------------------------------
// Author:      thuphuong.tran
// Create Date: 2010-07-14
// Description: Xử lý sự kiện upload anh sản phẩm
//-----------------------------------------------------------

function preLoad() {
    if (!this.support.loading) {
        jAlert(MESSAGE.UploadLogo.notPlayer, MESSAGE.title);
        return false;
    }
}
function loadFailed() {
    jAlert(MESSAGE.UploadLogo.loadFailed, MESSAGE.title);
}
function fileQueueError(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                jAlert(MESSAGE.UploadLogo.sizeLimit, MESSAGE.title);
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                jAlert(MESSAGE.UploadLogo.notExts, MESSAGE.title);
                break;
            default:
                jAlert(MESSAGE.UploadLogo.queueError, MESSAGE.title);
                break;
        }
    } catch (ex) {
        this.debug(ex);
    }

}
function clearQueue(obj) {
    var stats = obj.getStats();
    while (stats.files_queued > 0) {
        obj.cancelUpload();
        stats = obj.getStats();
    }
}
function fileQueued(file) {
    // check Exts
    var strfileExts = '*.jpg;*.png;*.gif';
    if (strfileExts.indexOf(file.type.toLowerCase()) == '-1') {
        jAlert(MESSAGE.UploadLogo.notExts, MESSAGE.title);
        clearQueue(this);
        return;
    }

    // check file size
    var byteSize = Math.round(file.size / 1024 * 100) * .01;
    if (parseInt(byteSize) > 2048) {
        jAlert(MESSAGE.UploadLogo.sizeLimit, MESSAGE.title);
        clearQueue(this);
        return;
    }
    $("#" + this.customSettings.fileName).html(file.name);
}
function fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        if (numFilesQueued > 1) {
            var stats = this.getStats();
            while (stats.files_queued > 0) {
                this.cancelUpload();
                stats = this.getStats();
            }
        }
        else {
            var stats = this.getStats();
            while (stats.files_queued > 1) {
                this.cancelUpload();
                stats = this.getStats();
            }
        }
    }
    catch (ex) {
        this.debug(ex);
    }
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
    var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
    var upload_message = Math.ceil(bytesLoaded / 1024) + 'kb/' + Math.ceil(bytesTotal / 1024) + 'kb';
    $('#' + this.customSettings.upload_status_bar).css('width', percent + '%');
    $('#' + this.customSettings.upload_message).html(upload_message);
    if (bytesLoaded == bytesTotal) {
        $('#' + this.customSettings.upload_message).html('watting...');
    }
}

function uploadSuccess(file, serverData) {
    if (serverData > 0) {
        //Pages_ListItemAdmin.bindListImageItem();
    }
    else { 
        $('.menuleft #imgLogo').attr('src', serverData);
        $('#' + this.customSettings.upload_message).html('');
    }
    Pages_ListItemAdmin.bindListImageItem();
    $(".selectFile .process .bg-process").css("background-color", "#FFFFFF");
}

function clearForm() {
    $('#_fileName').html('');
    $('#upload_status_bar').css('width', '0%');
    $('#upload_message').html('');
}
function uploadComplete(file) {
    try {

        var progress = new FileProgress(file, this.customSettings.upload_target);
        progress.setComplete();
        progress.toggleCancel(false);
        //        /*  I want the next upload to continue automatically so I'll call startUpload here */
        //        if (this.getStats().files_queued > 0) {
        //            this.startUpload();
        //        } else {
        //            var progress = new FileProgress(file, this.customSettings.upload_target);
        //            progress.setComplete();
        //            progress.toggleCancel(false);



        //        }
    } catch (ex) {
        this.debug(ex);
    }
}

function uploadError(file, errorCode, message) {
    var progress;
    try {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                try {
                    progress = new FileProgress(file, this.customSettings.upload_target);
                    progress.setCancelled();
                    progress.setStatus("Cancelled");
                    progress.toggleCancel(false);
                }
                catch (ex1) {
                    this.debug(ex1);
                }
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                try {
                    progress = new FileProgress(file, this.customSettings.upload_target);
                    progress.setCancelled();
                    progress.setStatus("Stopped");
                    progress.toggleCancel(true);
                }
                catch (ex2) {
                    this.debug(ex2);
                }
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                jAlert(MESSAGE.UploadLogo.uploadLimitExceeded, MESSAGE.title);
                break;
            case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                jAlert(MESSAGE.UploadLogo.httpError, MESSAGE.title);
                break;
            case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                jAlert(MESSAGE.UploadLogo.missingUploadUrl, MESSAGE.title);
                break;
            case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                jAlert(MESSAGE.UploadLogo.ioError, MESSAGE.title);
                break;
            case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                jAlert(MESSAGE.UploadLogo.securityError, MESSAGE.title);
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
                jAlert(MESSAGE.UploadLogo.uploadFailed, MESSAGE.title);
                break;
            default:
                jAlert(MESSAGE.UploadLogo.error + message, MESSAGE.title);
                break;
        }
    } catch (ex3) {
        this.debug(ex3);
    }

}
